import { vi } from 'vitest'
import { config } from '@vue/test-utils'

// Mock localStorage for Node environment
const localStorageMock = {
  getItem: vi.fn(),
  setItem: vi.fn(),
  removeItem: vi.fn(),
  clear: vi.fn(),
}

// Mock sessionStorage
const sessionStorageMock = {
  getItem: vi.fn(),
  setItem: vi.fn(),
  removeItem: vi.fn(),
  clear: vi.fn(),
}

// Mock matchMedia
const matchMediaMock = vi.fn().mockImplementation(query => ({
  matches: false,
  media: query,
  onchange: null,
  addListener: vi.fn(),
  removeListener: vi.fn(),
  addEventListener: vi.fn(),
  removeEventListener: vi.fn(),
  dispatchEvent: vi.fn(),
}))

// Mock ResizeObserver
const ResizeObserverMock = vi.fn().mockImplementation(() => ({
  observe: vi.fn(),
  unobserve: vi.fn(),
  disconnect: vi.fn(),
}))

// Mock IntersectionObserver
const IntersectionObserverMock = vi.fn().mockImplementation(() => ({
  observe: vi.fn(),
  unobserve: vi.fn(),
  disconnect: vi.fn(),
}))

// Mock performance API
const performanceMock = {
  now: vi.fn(() => Date.now()),
  mark: vi.fn(),
  measure: vi.fn(),
  clearMarks: vi.fn(),
  clearMeasures: vi.fn(),
  timing: {
    navigationStart: 1000,
    domainLookupStart: 1010,
    domainLookupEnd: 1020,
    connectStart: 1020,
    connectEnd: 1050,
    requestStart: 1050,
    responseEnd: 1200,
    domLoading: 1200,
    domContentLoadedEventEnd: 1400,
    loadEventEnd: 1500
  },
  navigation: {
    type: 0,
    redirectCount: 0
  },
  memory: {
    usedJSHeapSize: 10000000,
    totalJSHeapSize: 20000000,
    jsHeapSizeLimit: 100000000
  }
}

// Mock window and document for Node environment
Object.defineProperty(window, 'localStorage', {
  value: localStorageMock
})

Object.defineProperty(window, 'sessionStorage', {
  value: sessionStorageMock
})

Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: matchMediaMock
})

Object.defineProperty(window, 'ResizeObserver', {
  writable: true,
  value: ResizeObserverMock
})

Object.defineProperty(window, 'IntersectionObserver', {
  writable: true,
  value: IntersectionObserverMock
})

Object.defineProperty(window, 'performance', {
  writable: true,
  value: performanceMock
})

// Mock navigator
Object.defineProperty(window, 'navigator', {
  value: {
    userAgent: 'test',
    clipboard: {
      writeText: vi.fn().mockResolvedValue(undefined)
    }
  }
})

// Mock document methods
Object.defineProperty(document, 'documentElement', {
  value: {
    setAttribute: vi.fn(),
    classList: {
      add: vi.fn(),
      remove: vi.fn(),
      contains: vi.fn(),
      toggle: vi.fn()
    },
    style: {
      setProperty: vi.fn(),
      removeProperty: vi.fn()
    }
  }
})

Object.defineProperty(document, 'body', {
  value: {
    classList: {
      add: vi.fn(),
      remove: vi.fn(),
      contains: vi.fn(),
      toggle: vi.fn()
    }
  }
})

// Configure Vue Test Utils
config.global.stubs = {
  'el-icon': true,
  'el-button': true,
  'el-input': true,
  'el-select': true,
  'el-option': true,
  'el-checkbox': true,
  'el-radio-group': true,
  'el-radio-button': true,
  'el-switch': true,
  'el-progress': true,
  'el-card': true,
  'el-drawer': true,
  'el-empty': true,
  'el-skeleton': true,
  'el-alert': true,
  'el-tag': true,
  'el-tooltip': true,
  'el-button-group': true,
  'el-dropdown': true,
  'el-dropdown-menu': true,
  'el-dropdown-item': true,
  'el-tree': true,
  'el-color-picker': true,
  'el-input-number': true,
  'el-descriptions': true,
  'el-descriptions-item': true,
  'el-scrollbar': true,
  'el-text': true,
  'el-pagination': true,
  'el-message-box': true
}

// Mock console methods to reduce noise in tests
global.console = {
  ...console,
  warn: vi.fn(),
  error: vi.fn(),
  log: vi.fn(),
}